"""
This class handles files related to iTOL
See the README.txt for details
@author: Albert Wang (albertyw@mit.edu)
@date: September 16, 2010

@TODO: Use python's builtin tempfile function:
http://docs.python.org/library/tempfile.html
"""

import os
import tempfile

class ItolFile:
    """
    This class handles files related to the iTOL API
    """
    def __init__(self):
        """
        Create the class
        """
        self.temp_file_location = 'itol_tempfile'
        self.file_location = ''
        self.file_contents = ''
    
    def set_file_location(self, file_location):
        """
        Set the file location (assumes not a temp File)
        @param fileLocation: the path to the file
        """
        file_handle = open(file_location,'r')
        self.file_contents = file_handle.read()
        file_handle.close()
    
    def set_file_contents(self, file_contents):
        """
        Set File Contents (stored in a temporary file)
        @param filecontents: Get the contents to store in a temporary file
        """
        self.file_contents = file_contents
    
    def get_file_contents(self):
        """
        Read the File Contents
        @return: the contents of the file
        """
        return self.file_contents
        
    def get_file_handle(self):
        """
        Get the file handle in preparation for uploading
        @return a file handle
        """
        temp_handle = tempfile.TemporaryFile('r+')
        temp_handle.write(self.file_contents)
        temp_handle.seek(0)
        return temp_handle
        
